//
//  Double+Additions.swift
//  MGExtensionsKit
//
//  Created by Max on 2020/5/20.
//

import Foundation

extension Extensive where Base == Double {
    
    public var minsAndSecsFormatString: String {
        let min = UInt(base / 60)
        let sec = UInt(base) - min * 60
        return String(format: "%02ld:%02ld", min, sec)
    }
    
    /// 6.0 -> 6
    /// 6.1 -> 6.1
    public var formatedStringWithoutDecimalZero: String {
        let string = "\(base)"
        let components = string.split(separator: ".")
        guard components.count == 2 else {
            return string
        }
        if components[1] == "0" {
            return String(format: "%.0f", base)
        }
        return string
    }
    
}

public extension TimeInterval {
    func dateFormated(_ format: String? = nil, locale: Locale = Locale.current) -> String {
         let date = Date(timeIntervalSince1970: self)
         let df = DateFormatter()
         df.locale = locale
         
         // 如果提供了 format，则直接使用它
         if let format = format {
             df.dateFormat = format
         } else {
             // 默认格式化逻辑，根据 locale 设置
             if locale.identifier == "zh_CN" {
                 df.dateFormat = "yyyy-MM-dd" // 国内格式
             } else {
                 df.dateFormat = "MM-dd-yyyy" // 国外格式
             }
         }
         
         return df.string(from: date)
     }
}
